//
//  SureGroupIndentViewController.m
//  PeopleStreet
//
//  Created by 辰领科技 on 16/4/1.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "SureGroupIndentViewController.h"
#import "ScroButton.h"
#import "PayViewController.h"

@interface SureGroupIndentViewController ()
@property (strong, nonatomic) IBOutlet UIScrollView *scroView;
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIView *codePayView;
@property (strong, nonatomic) IBOutlet UIButton *maskView;
@property (strong, nonatomic) IBOutlet UIButton *addButton;

@property (strong, nonatomic) IBOutlet UILabel *tiePhoneNumber;
@property (strong, nonatomic) IBOutlet UILabel *codePayLabel;
@property (strong, nonatomic) IBOutlet UILabel *goodsPrice;
@property (strong, nonatomic) IBOutlet UIImageView *goodsImage;
@property (strong, nonatomic) IBOutlet UILabel *storeName;
@property (strong, nonatomic) IBOutlet UILabel *goodsInfo;
@property (strong, nonatomic) IBOutlet UIButton *deleteButton;
@property (strong, nonatomic) IBOutlet UILabel *goodsNumber;
@property (strong, nonatomic) IBOutlet UILabel *totalNumber;
@property (strong, nonatomic) IBOutlet UITextField *codePayField;
@property(nonatomic,assign)NSInteger ifCodePayIndex;//是否抵扣积分53 54 55
@property(nonatomic,assign)NSInteger goodsNum;
@property(nonatomic,strong)ScroButton * scroButton;
@end

@implementation SureGroupIndentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _ifCodePayIndex = 53;
    _goodsNum = 1;
    // Do any additional setup after loading the view.
    [self initViewLayout];
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroView];
    [self adaptiveViewLayout:self.codePayView];
    _scroView.contentSize =  flexibleSize(CGSizeMake(0, 462), YES);
    _addButton.layer.masksToBounds = YES;
    _addButton.layer.borderColor = [UIColor colorWithWhite:0.957 alpha:1.000].CGColor;
    _addButton.layer.borderWidth = 1;
    
    _deleteButton.layer.masksToBounds = YES;
    _deleteButton.layer.borderColor = [UIColor colorWithWhite:0.957 alpha:1.000].CGColor;
    _deleteButton.layer.borderWidth = 1;
    
    _goodsNumber.layer.masksToBounds = YES;
    _goodsNumber.layer.borderColor = [UIColor colorWithWhite:0.957 alpha:1.000].CGColor;
    _goodsNumber.layer.borderWidth = 1;
    
    _scroButton = [[ScroButton alloc]initWithFrame:flexibleFrame(CGRectMake(263, 138, 49, 23), YES) title:@"" selectedTitle:@"" selectedColor:[UIColor colorWithRed:0.988 green:0.580 blue:0.055 alpha:1.000] cancelColor:[UIColor colorWithWhite:0.620 alpha:1.000] selected:NO oval:(BOOL)NO complect:^(NSInteger index) {
        
        if (index == 1) {
            _maskView.hidden = NO;
            _codePayView.hidden = NO;
        }
        
    }];
    [_scroView addSubview:_scroButton];
    
    
    //        NSString * image = dic[@"storeImg"];
    //        if (image) {
    //            [cell.shoppingImage sd_setImageWithURL:[NSURL URLWithString:image]];
    //        }else{
    //            cell.shoppingImage.image = [UIImage imageNamed:@"gsInfo1"];
    //        }
    
    _storeName.text = _shopInfoDic[@"storeName"];
    _goodsInfo.text = [NSString stringWithFormat:@"%ld人餐，节假日通用！",[[Common stringFromData:_shopInfoDic[@"mgmnNumber"]] integerValue]];

    NSString * price = [NSString stringWithFormat:@"¥%@",_shopInfoDic[@"mgpiGourponAmount"]];
    NSString * oldPrice = [NSString stringWithFormat:@"¥%@",_shopInfoDic[@"mgpiOrginalAmount"]];
    NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@  %@",price,oldPrice]];
    [str addAttribute:NSForegroundColorAttributeName value:kGetColor(240, 59, 59) range:NSMakeRange(0, price.length)];
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15*VerticalTexeRatio()] range:NSMakeRange(0, price.length)];
    [str addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(str.length-oldPrice.length, oldPrice.length)];
    _goodsPrice.attributedText = str;

    
    
    NSString * number = [NSString stringWithFormat:@"共%d件,总价:",1];
    NSString * total = [NSString stringWithFormat:@"¥%.2f",[_shopInfoDic[@"mgpiGourponAmount"] floatValue]];
    NSMutableAttributedString * Str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@",number,total]];
    [Str addAttribute:NSForegroundColorAttributeName value:kGetColor(240, 59, 59) range:NSMakeRange(number.length, total.length)];
    [Str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18*VerticalTexeRatio()] range:NSMakeRange(number.length, total.length)];
    _totalNumber.attributedText =Str;

}


- (IBAction)back:(id)sender {
    [self.navigationController  popViewControllerAnimated:YES];
}

#pragma mark == 商品数量减
- (IBAction)addHandel:(id)sender {
    if (_goodsNum >1) {
       _goodsNum--;
    }
    
    _goodsNumber.text = [NSString stringWithFormat:@"%ld",_goodsNum];
    
    NSString * number = [NSString stringWithFormat:@"共%ld件,总价:",_goodsNum];
    NSString * total = [NSString stringWithFormat:@"¥%.2f",[_shopInfoDic[@"mgpiGourponAmount"] floatValue]*_goodsNum];
    NSMutableAttributedString * Str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@",number,total]];
    [Str addAttribute:NSForegroundColorAttributeName value:kGetColor(240, 59, 59) range:NSMakeRange(number.length, total.length)];
    [Str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18*VerticalTexeRatio()] range:NSMakeRange(number.length, total.length)];
    _totalNumber.attributedText =Str;
}

#pragma mark == 商品数量加
- (IBAction)deleteHandel:(id)sender {
    _goodsNum++;
    _goodsNumber.text = [NSString stringWithFormat:@"%ld",_goodsNum];
    
    NSString * number = [NSString stringWithFormat:@"共%ld件,总价:",_goodsNum];
    NSString * total = [NSString stringWithFormat:@"¥%.2f",[_shopInfoDic[@"mgpiGourponAmount"] floatValue]*_goodsNum];
    NSMutableAttributedString * Str = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@%@",number,total]];
    [Str addAttribute:NSForegroundColorAttributeName value:kGetColor(240, 59, 59) range:NSMakeRange(number.length, total.length)];
    [Str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18*VerticalTexeRatio()] range:NSMakeRange(number.length, total.length)];
    _totalNumber.attributedText =Str;

}

- (IBAction)payHandel:(id)sender {
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:_groupShoppingId forKey:@"mgprId"];
    if (_scroButton.ifSelected==YES) {
        [dic setObject:@"" forKey:@"integral"];
        if (_ifCodePayIndex==53) {
            [dic setObject:@"0" forKey:@"juge"];
        }else if (_ifCodePayIndex==54){
        [dic setObject:@"1" forKey:@"juge"];
        }else{
            [dic setObject:@"2" forKey:@"juge"];
             [dic setObject:_codePayField.text forKey:@"integral"];
 
        }
        
    }else{
     [dic setObject:@"0" forKey:@"juge"];
    }
    
    [dic setObject:[NSString stringWithFormat:@"%ld",_goodsNum] forKey:@"mgoeCount"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:SX_appSavaGrouponOrder withHttpType:@"POST" withTag:SX_SX_appSavaGrouponOrder];
}

- (IBAction)closeCodeView:(id)sender {
    _codePayView.hidden = YES;
    _maskView.hidden = YES;
}

#pragma mark == 选择是否抵扣积分
- (IBAction)ifCodePay:(UIButton *)sender {
    sender.selected = YES;
    if (sender.tag == _ifCodePayIndex) {
        return;
    }
    UIButton * button = [self.view viewWithTag:_ifCodePayIndex];
    button.selected = NO;
    _ifCodePayIndex = sender.tag;
    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
}

#pragma mark == 确定是否抵扣积分
- (IBAction)sureIfCodePay:(id)sender {
    _maskView.hidden = YES;
    _codePayView.hidden = YES;
    
}



#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag == SX_SX_appSavaGrouponOrder){
        if (  [[database valueForKey:@"type"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:2];
            UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"HomeStoryboard" bundle:nil];
            PayViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Pay"];
            page.payOrderName = @"凡人街在线商城";
            page.payOrderPrice =  [NSString stringWithFormat:@"%.2f",[_shopInfoDic[@"mgpiGourponAmount"] floatValue]*_goodsNum];
            page.type = 2;
            page.payId = database[@"result"][@"mgodNo"];
            [self.navigationController   pushViewController:page animated:YES];
        }
    }
}


-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    
    //
    [self dismissWaitDialog];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
